﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms.Integration;
using UnityEditor.Layout;

namespace UnityEditor.Controls.GameGraphicsControl
{
    /// <summary>
    /// Interaction logic for GameGraphicsControl.xaml
    /// </summary>
    public partial class GameGraphicsControl : UserControl
    {
        public GraphicsGame Game
        {
            set
            {
                child.Game = value;
            }
        }
        private readonly WinForm child = new WinForm();
        private WindowsFormsHost windowsFormsHost = new WindowsFormsHost();

        public GameGraphicsControl()
        {
            InitializeComponent();

            this.Initialize();
        }

        public void Initialize()
        {
            stackPanel.Width = child.Width;
            stackPanel.Height = child.Height;
            windowsFormsHost.Width = child.Width;
            windowsFormsHost.Height = child.Height;

            child.TopLevel = false;
            windowsFormsHost.Child = child;

            stackPanel.Children.Add(windowsFormsHost);

            Workspace.Instance.GameChanged += new EventHandler(Instance_GameChanged);
        }

        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            child.ClientSize = new System.Drawing.Size((int)this.ActualWidth, (int)this.ActualHeight);

            stackPanel.Width = child.Width;
            stackPanel.Height = child.Height;
            windowsFormsHost.Width = child.Width;
            windowsFormsHost.Height = child.Height;
        }

        private void Instance_GameChanged(Object sender, EventArgs e)
        {
            this.Game = Workspace.Instance.GameGraphicsGame;
        }
    }
}
